package pers.xuyong.strive.datastruct.order.sample;

import pers.xuyong.strive.datastruct.order.AbstractOrder;
import pers.xuyong.strive.datastruct.order.Order;

/**
 * 选择排序
 *
 * @author billymac
 * @version 1.0
 * @date 17/5/23  下午3:16
 * @since JDK1.8
 */
public class SelectorOrder<E extends Number> extends AbstractOrder<E>
            implements Order<E>{
    @Override
    public void sort(E[] ts, int method) {
        valid(ts);
        for (int i = 0; i < ts.length; i++) {
            int index = i;
            for (int j = i + 1; j < ts.length; j++) {
                if (method > 0 && ts[index].doubleValue() > ts[j].doubleValue() ||
                        method < 0 && ts[index].doubleValue() < ts[j].doubleValue())
                    index = j;
            }
            if (i != index)
                swap(ts, i, index);
        }
    }
}
